﻿$(function() {
    var piebar = echarts.init(document.getElementById('barweek'));
    optionbar = {
		tooltip : {
	        trigger: 'axis',
	        axisPointer: {
		        type: 'shadow'
		    }
	    },
	    grid: {
            top: '15%',
            left: '5%',
            right: '5%',
            bottom:'15%',
            containLabel: true
        },
        backgroundColor:'#e0e0e0',
        color:['#ff6264','#cc3f44'],
        legend: {
	    	bottom:25,
	        data:['每周报警时长(分钟)','相对报警(%)']
	    },
	    xAxis : [{
            type : 'category',
            axisLabel : {
                formatter: '{value}周'
            },
            data : [],
            splitLine:{
            	show:false
            }
        }],
	    yAxis : [{
	    	min:0,
	        type : 'value',
            name : '每周报警时长(分钟)',
            axisLabel : {
                formatter: '{value}'
            },
            splitLine:{
            	show:false
            }
        }, {
        	min:0,
        	max:100,
            type : 'value',
            name : '相对报警时长(报警时长/工作总时长)',
            axisLabel: {
            	formatter: '{value}%'
            },
            splitLine:{
            	show:false
            }
        }],
	    series : [{
            name:'每周报警时长(分钟)',
            barMaxWidth:80,
            type:'bar',
            barMaxWidth:60,
            data:[]
        },{
            name:'相对报警(%)',
            yAxisIndex: 1,
            type:'line',
            data:[]
        }]
    };
    piebar.setOption(optionbar);
    
    
    var lineId=0;var equId=0;
    var	startWeek="";
    var	endWeek="";
    var year=moment().year();
    
    /**
     * slider滑动事件
     * */
    $('#ss').slider({
    	range: true,
        onComplete:function(value){//滑块值改变时触发
	       	startWeek=value[0];
	       	endWeek=value[1];
	       	getData();
       }
    });
    
    /**
     * 初始化
     * */
    var vm = new Vue({
	    el: '#itemData',
	    data:{item:[]}
	}); 

    var currentWeek=moment().week();
    var weeks="";
    if(currentWeek<8){
    	weeks=[1,currentWeek];
    }else{
    	weeks=[moment().subtract(7, 'w').week(),currentWeek];
    }
    $('#ss').slider('setValues',weeks);
    var arr=$('#ss').slider('getValues');
    startWeek=arr[0];
    endWeek=arr[1];
    getData();
    
    function getData(){
    	$.post(basePath+"/single/singlePower/week.json",{"lineId":lineId,"equId":equId,"year":year,"startTime":startWeek,"endTime":endWeek},function(res){
    		vm.$set('item',res);
    			piebar.setOption({
			        xAxis: {
			            data: res.weeks
			        },
			        series: [{
			        	name:"每周报警时长(分钟)",
			            data: res.overs
			        },{
			        	name:"相对报警(%)",
			            data: res.relativeOvers
			        }]
    			});
    	});
    }

    
	 //点击事件
	 $("#defineTime").click(function (){
		 var mePanel = $('#defineTimeWin');
	   	 mePanel.dialog('open');
	 });
	//定义时间的表单
	$('#defineTimeWin').dialog({
		title:'自定义时间',
		width:400,
		height:150,
		iconCls:'icon-add',
		closed: true,
		modal: true,
		inline:true,
		buttons:[{
			text:'确定',
			iconCls:'icon-add-new',
			handler:function(){
					var st=$("input[name='startTime']").val();
					var cmax=moment(st, "YYYY").weeksInYear();
					year=st;
					$('#ss').slider({
						max:cmax
					});
					getData();
					$('#defineTimeWin').dialog('close');

			}
		},{
			text:'取消',
			iconCls:'icon-redo',
			handler:function(){
				$('#defineTimeWin').dialog('close');
			}
		}],
		onClose: function() {
			$('#defineTimeform').form('reset');
		}
	});
	 /**
	  * 切换设备的点击事件
	  * */
	var mePanel = $('#switchEquip');
	$("#linetree").tree({
		onClick: function(node) {
			if (node.url) {
				lineId=node.id;
				var lineGrid = $('#lines_data');
				lineGrid.datagrid('load',{
					lineId:node.id
				});
				lineGrid.datagrid('getPanel').panel('setTitle',node.text);
			}
		}
	});
	$('#lines_data').datagrid({
		//双击设备时，根据选中设备刷新
		onDblClickRow: function(rowIndex, rowData){
			lineId=rowData.lineId;
			equId=rowData.id;
			var arr=$('#ss').slider('getValues');
		    startWeek=arr[0];
		    endWeek=arr[1];
		    getData();
		    mePanel.dialog('close');
		}
	});
  $("#switchEquipOK").click(function(){
  	var row = $('#lines_data').datagrid('getSelected');
  	if(row){
  		lineId=row.lineId;
  		equId=row.id;
  		var arr=$('#ss').slider('getValues');
  	    startWeek=arr[0];
  	    endWeek=arr[1];
  	    getData();
		$('#switchEquip').dialog('close');
  	}else{
  		$.messager.alert({showSpeed:100,title:'消息提示',msg:'请选择一条设备记录!',timeout:3000});
  	}
  });
});